package com.yunhe.marketing.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yunhe.common.model.mybatis.PageResult;
import com.yunhe.marketing.domain.CompanyHeat;
import com.yunhe.marketing.repository.CompanyHeatRepository;
import com.yunhe.marketing.service.ICompanyHeatService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

/**
 * @ClassName: CompanyHeatServiceImpl
 * @Description:
 * @author: GongDaCheng
 * @Date: 14:11 2021/3/15
 */
@Service
public class CompanyHeatServiceImpl implements ICompanyHeatService {

    private Logger logger = Logger.getLogger(CompanyHeatServiceImpl.class);

    @Autowired
    CompanyHeatRepository companyHeatRepository;

    @Override
    public CompanyHeat selectHeatCompany(Long itemId) {
        return companyHeatRepository.selectHeatCompany(itemId);
    }

    @Override
    public PageResult selectHeatCompanyList(int page, int size) {
        PageHelper.startPage(page,size);
        List<CompanyHeat> companyHeats = companyHeatRepository.selectHeatCompanyList();
        PageInfo<CompanyHeat> companyHeatPageInfo = new PageInfo<>(companyHeats);
        return new PageResult(page,size, (int) companyHeatPageInfo.getTotal(), companyHeats);
    }

    @Override
    public List<CompanyHeat> selectAllHeatCompanyList() {
        List<CompanyHeat> companyHeats = companyHeatRepository.selectHeatCompanyList();
        return companyHeats;
    }

    @Override
    public void saveOrUpdate(CompanyHeat companyHeat) {
        CompanyHeat existCompanyHeat = selectHeatCompany(companyHeat.getItemId());
        logger.info("CompanyHeat: " + companyHeat.toString());
        if (existCompanyHeat != null) {
            logger.info("CompanyHeat is exist ");
            companyHeatRepository.updateCompanyHeat(companyHeat);
        } else {
            logger.info("CompanyHeat is not exist ");
            companyHeatRepository.addCompanyHeat(companyHeat);
        }
    }
}
